<?php

/**
 * @Author: king
 * @Date:   2019-05-04 17:55:50
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-12 11:48:36
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;
/**
 * 准了货品属性相关模型
 * Class LanmeiGoodsCommodityAttr
 * @package app\common\model
 */
class LanmeiGoodsCommodityAttr extends Base
{
    protected $table = 'astro_goods_commodity_attr';

    const TAG = 'commodity_attr';
    const COMMODITY_ATTR_IDS = 'commodity:attr:ids:'; // 货品属性的id缓存键名
    const COMMODITY_ATTR_INFO = 'commodity:attr:info:';//货品属性详情缓存键名
    const CACHE_TIME = 86400 * 7;

    /**
     * 获取相关货品的属性
     * @param array $map
     * @return array|bool
     */
    public function getCommodityAttr($map = [])
    {
        $commid = $map['commid'];

        $caids = false;
        $data_list=[];

        if ($this->cache_on >= 1 && Cache::has(self::COMMODITY_ATTR_IDS . $commid)) {
            $caids = Cache::get(self::COMMODITY_ATTR_IDS . $commid);
        }
        if ($caids === false) {
            try {
                $where = ['status' => 1, 'caid' => $commid];
                $data = $this
                    ->where($where)
                    ->get(['id']);
                if ($data) {
                    $caids = implode(',', $data);
                }

                if ($this->cache_on >= 1 && $caids) {
                    Cache::tag(self::TAG)->set(self::COMMODITY_ATTR_IDS . $commid, $caids, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('获取生时矫正货品属性失败', config('code.error'));
            }

           $caids = explode(',',$caids);

            if (!empty($caids))
            {
                $ac_ids = '';
                $temp =[];

                foreach ($caids as $ac_id)
                {
                    if ($this->cache_on>=1 && Cache::has(self::COMMODITY_ATTR_INFO.$ac_id))
                    {
                        $temp[$ac_id] = Cache::get(self::COMMODITY_ATTR_INFO.$ac_id);
                    }else{
                        $temp[$ac_id] = false;
                    }

                    if ($temp[$ac_id] === false && $ac_id)
                    {
                        $ac_ids .= $ac_id .',';
                    }
                }

                if ($ac_ids !== '' && strlen($ac_ids)>0)
                {
                    //去掉结尾的 ，
                    $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);

                    try{
                        $where = ['gct.status' => 1];
                        $data_info = $this
                            ->from('astro_goods_commodity_attr as gct')
                            ->select('gct.id','gct.name','gct.or_price','gct.now_price','gct.inventory','gct.attr','gct.description','ac.status','ac.type_id','ac.name','ac.attr','ac.start_time','ac.end_time')
                            ->join('discout as d', 'gct.id','=','d.gctid', 'LEFT')
                            ->join('activity as ac', 'd.acid','=','ac.id', 'INNER')
                            ->where($where)
                            ->whereIn('gct.id',$ac_ids)
                            ->orderBy('gct.sort', 'DESC')
                            ->get();
                        $data_info = json_decode(json_encode($data_info,true),true);
                    }catch (\Exception $e)
                    {
                        throw new ApiException('获取货品属性失败', config('code.error'));
                    }

                    if (!empty($data_info))
                    {
                        foreach ($data_info as $c)
                        {
                            if ($this->cache_on>=1)
                            {
                                Cache::tag(self::TAG)->set(self::COMMODITY_ATTR_INFO.$c['id'],$c,self::CACHE_TIME);
                            }

                            if ($c)
                            {
                                $temp[$c['id']] = $c;
                            }
                        }
                    }

                    if (!empty($temp))
                    {
                        foreach ($temp as $item)
                        {
                            $data_list[]=$item;
                        }
                    }
                }
            }

        }
        return $data_list;
    }

    /**
     * 获取具体货品属性信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getCommodityInfo($map=[])
    {
        $id= $map['id'];
        $data = false;

        if ($this->cache_on>=1 && Cache::has(self::COMMODITY_ATTR_INFO.$id))
        {
            $data = Cache::get(self::COMMODITY_ATTR_INFO.$id);
        }
        if ($data === false)
        {
            try{
                $where = ['gct.id' => $id, 'gct.status' => 1];
                $data = $this
                    ->from('astro_goods_commodity_attr as gct')
                    ->select('gct.id','gct.name as gname','gct.or_price','gct.now_price','gct.inventory','gct.attr as gattr','gct.description','ac.status','ac.type_id','ac.name as aname','ac.attr as aattr','ac.start_time','ac.end_time')
                    ->join('discount as d', 'gct.id','=','d.gctid', 'LEFT')
                    ->join('activity as ac', 'd.acid','=','ac.id', 'LEFT')
                    ->where($where)
                    ->firstOrFail();
                $data = json_decode(json_encode($data,true),true);
                if ($this->cache_on>=1)
                {
                    Cache::tag(self::TAG)->set(self::COMMODITY_ATTR_INFO.$id,$data,self::CACHE_TIME);
                }
            }catch (\Exception $e)
            {
               throw new ApiException('获取货品属性失败', config('code.error'));
            }
        }

        return $data;
    }
}
